﻿using StartEAS_API.Utils;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.DAL.WorkerEvaluationD
{
    public class WorkerEvaluationDAL
    {
        private readonly StarsEASContext _context;

        public WorkerEvaluationDAL(StarsEASContext context) { 
            _context = context;
        }

        public ResultUtils GetWorkerEvaluation(string token) {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"查询教评表---账户{token}尝试登录但登录失败", true);
                return new ResultUtils() { code = 500, message = "Token错误" };
            }
            if (user.PositionId != 6)
            {
                var workerEvaluationList = from workerEvaluation in _context.WorkerEvaluations
                                           join worker in _context.Workers on workerEvaluation.WorkerId equals worker.WorkerId
                                           join course in _context.Courses on workerEvaluation.CourseId equals course.CourseId
                                           where workerEvaluation.StuId == user.Username
                                           select new
                                           {
                                               WorkerEvaluationId = workerEvaluation.WorkerEvaluationId,
                                               WorkerId = workerEvaluation.WorkerId,
                                               WorkerName = worker.WorkerName,
                                               StuId = workerEvaluation.StuId,
                                               CourseId = workerEvaluation.CourseId,
                                               CourseName = course.CourseName,
                                               WorkerEvaluationOption1 = workerEvaluation.WorkerEvaluationOption1,
                                               WorkerEvaluationOption2 = workerEvaluation.WorkerEvaluationOption2,
                                               WorkerEvaluationOption3 = workerEvaluation.WorkerEvaluationOption3,
                                               WorkerEvaluationOption4 = workerEvaluation.WorkerEvaluationOption4,
                                               WorkerEvaluationOption5 = workerEvaluation.WorkerEvaluationOption5,
                                               EvaluationOfTime = workerEvaluation.EvaluationOfTime
                                           };
                if (workerEvaluationList == null) {
                    AppLog.WriteError($"查询教评表---非教务处未查询到{user.Username}的教评", true);
                    return new ResultUtils() { code = 500, message = "您所查询的数据为空" };
                }
                AppLog.WriteError($"查询教评表---非教务处查询到{workerEvaluationList.Count()}条教评", true);
                return new ResultUtils() { code = 200, message = workerEvaluationList };
            }
            else {
                var workerEvaluationList = from workerEvaluation in _context.WorkerEvaluations
                                           join worker in _context.Workers on workerEvaluation.WorkerId equals worker.WorkerId
                                           join course in _context.Courses on workerEvaluation.CourseId equals course.CourseId
                                           select new
                                           {
                                               WorkerEvaluationId = workerEvaluation.WorkerEvaluationId,
                                               WorkerId = workerEvaluation.WorkerId,
                                               WorkerName = worker.WorkerName,
                                               StuId = workerEvaluation.StuId,
                                               CourseId = workerEvaluation.CourseId,
                                               CourseName = course.CourseName,
                                               WorkerEvaluationOption1 = workerEvaluation.WorkerEvaluationOption1,
                                               WorkerEvaluationOption2 = workerEvaluation.WorkerEvaluationOption2,
                                               WorkerEvaluationOption3 = workerEvaluation.WorkerEvaluationOption3,
                                               WorkerEvaluationOption4 = workerEvaluation.WorkerEvaluationOption4,
                                               WorkerEvaluationOption5 = workerEvaluation.WorkerEvaluationOption5,
                                               EvaluationOfTime = workerEvaluation.EvaluationOfTime,
                                           };
                if (workerEvaluationList == null)
                {
                    AppLog.WriteError($"查询教评表---教务处未查询到{user.Username}的教评", true);
                    return new ResultUtils() { code = 500, message = "您所查询的数据为空" };
                }
                AppLog.WriteError($"查询教评表---教务处查询到{workerEvaluationList.Count()}条教评", true);
                return new ResultUtils() { code = 200, message = workerEvaluationList };
            }
        }

        public IEnumerable<Object> GetWorkerEvaluationsByWorkerId(string token, string workerId) {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"查询教评表---{token}未查询到用户信息", true);
                return null;
            }
            if (user.PositionId != 6)
            {
                AppLog.WriteError($"查询教评表---{token}为非教务处账户", true);
                return null;
            }
            if (workerId == null)
            {
                AppLog.WriteError($"查询教评表---workerId未接收到值", true);
                return null;
            }
            var workerEvaluationList = from workerEvaluation in _context.WorkerEvaluations
                                       join worker in _context.Workers on workerEvaluation.WorkerId equals worker.WorkerId
                                       join course in _context.Courses on workerEvaluation.CourseId equals course.CourseId
                                       where workerEvaluation.WorkerId == workerId
                                       select new
                                       {
                                           WorkerEvaluationId = workerEvaluation.WorkerEvaluationId,
                                           WorkerId = workerEvaluation.WorkerId,
                                           WorkerName = worker.WorkerName,
                                           StuId = workerEvaluation.StuId,
                                           CourseId = workerEvaluation.CourseId,
                                           CourseName = course.CourseName,
                                           WorkerEvaluationOption1 = workerEvaluation.WorkerEvaluationOption1,
                                           WorkerEvaluationOption2 = workerEvaluation.WorkerEvaluationOption2,
                                           WorkerEvaluationOption3 = workerEvaluation.WorkerEvaluationOption3,
                                           WorkerEvaluationOption4 = workerEvaluation.WorkerEvaluationOption4,
                                           WorkerEvaluationOption5 = workerEvaluation.WorkerEvaluationOption5,
                                           EvaluationOfTime = workerEvaluation.EvaluationOfTime
                                       };
            if (workerEvaluationList == null)
            {
                AppLog.WriteError($"查询教评表---根据教职工编号{workerId}未查询到教评", true);
                return null;
            }
            AppLog.WriteInfo($"查询教评表---根据教职工编号{workerId}查询到{workerEvaluationList.Count()}条教评", true);
            return workerEvaluationList;
        }

        public IEnumerable<Object> GetWorkerEvaluationsByStuId(string token, string StuId)
        {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"查询教评表---{token}未查询到用户信息", true);
                return null;
            }
            if (user.PositionId != 6)
            {
                AppLog.WriteError($"查询教评表---{token}为非教务处账户", true);
                return null;
            }
            if (StuId == null)
            {
                AppLog.WriteError($"查询教评表---StuId未接收到值", true);
                return null;
            }
            var workerEvaluationList = from workerEvaluation in _context.WorkerEvaluations
                                       join worker in _context.Workers on workerEvaluation.WorkerId equals worker.WorkerId
                                       join course in _context.Courses on workerEvaluation.CourseId equals course.CourseId
                                       where workerEvaluation.StuId == StuId
                                       select new
                                       {
                                           WorkerEvaluationId = workerEvaluation.WorkerEvaluationId,
                                           WorkerId = workerEvaluation.WorkerId,
                                           WorkerName = worker.WorkerName,
                                           StuId = workerEvaluation.StuId,
                                           CourseId = workerEvaluation.CourseId,
                                           CourseName = course.CourseName,
                                           WorkerEvaluationOption1 = workerEvaluation.WorkerEvaluationOption1,
                                           WorkerEvaluationOption2 = workerEvaluation.WorkerEvaluationOption2,
                                           WorkerEvaluationOption3 = workerEvaluation.WorkerEvaluationOption3,
                                           WorkerEvaluationOption4 = workerEvaluation.WorkerEvaluationOption4,
                                           WorkerEvaluationOption5 = workerEvaluation.WorkerEvaluationOption5,
                                           EvaluationOfTime = workerEvaluation.EvaluationOfTime
                                       };
            if (workerEvaluationList == null)
            {
                AppLog.WriteError($"查询教评表---根据学生编号{StuId}未查询到教评", true);
                return null;
            }
            AppLog.WriteInfo($"查询教评表---根据学生编号{StuId}查询到{workerEvaluationList.Count()}条教评", true);
            return workerEvaluationList;
        }

        public IEnumerable<Object> GetWorkerEvaluationsByCourseId(string token, int CourseId)
        {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"查询教评表---{token}未查询到用户信息", true);
                return null;
            }
            if (user.PositionId != 6)
            {
                AppLog.WriteError($"查询教评表---{token}为非教务处账户", true);
                return null;
            }
            if (CourseId == null)
            {
                AppLog.WriteError($"查询教评表---CourseId未接收到值", true);
                return null;
            }
            var workerEvaluationList = from workerEvaluation in _context.WorkerEvaluations
                                       join worker in _context.Workers on workerEvaluation.WorkerId equals worker.WorkerId
                                       join course in _context.Courses on workerEvaluation.CourseId equals course.CourseId
                                       where workerEvaluation.CourseId == CourseId
                                       select new
                                       {
                                           WorkerEvaluationId = workerEvaluation.WorkerEvaluationId,
                                           WorkerId = workerEvaluation.WorkerId,
                                           WorkerName = worker.WorkerName,
                                           StuId = workerEvaluation.StuId,
                                           CourseId = workerEvaluation.CourseId,
                                           CourseName = course.CourseName,
                                           WorkerEvaluationOption1 = workerEvaluation.WorkerEvaluationOption1,
                                           WorkerEvaluationOption2 = workerEvaluation.WorkerEvaluationOption2,
                                           WorkerEvaluationOption3 = workerEvaluation.WorkerEvaluationOption3,
                                           WorkerEvaluationOption4 = workerEvaluation.WorkerEvaluationOption4,
                                           WorkerEvaluationOption5 = workerEvaluation.WorkerEvaluationOption5,
                                           EvaluationOfTime = workerEvaluation.EvaluationOfTime
                                       };
            if (workerEvaluationList == null)
            {
                AppLog.WriteError($"查询教评表---根据课程编号{CourseId}未查询到教评", true);
                return null;
            }
            AppLog.WriteInfo($"查询教评表---根据课程编号{CourseId}查询到{workerEvaluationList.Count()}条教评", true);
            return workerEvaluationList;
        }


        public int AddWorkerEvaluation(string token, WorkerEvaluation workerEvaluation) {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();
            if (user == null)
            {
                AppLog.WriteError($"添加教评表---{token}未查询到用户信息", true);
                return -1;
            }
            if (workerEvaluation == null)
            {
                AppLog.WriteError($"添加教评表---workerEvaluation为空", true);
                return -1;
            }
            workerEvaluation.WorkerEvaluationId = 0;
            workerEvaluation.EvaluationOfTime = DateTime.Now.AddHours(8);
            _context.WorkerEvaluations.Add(workerEvaluation);
            int result = _context.SaveChanges();
            if (result == 0) {
                AppLog.WriteError($"添加教评表---{user.Username}添加失败", true);
                return 0;
            }
            AppLog.WriteInfo($"添加教评表---{user.Username}添加成功", true);
            return result;
        }




    }
}
